package com.usernet.product.dao;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.usernet.product.comm.BaseDao;
import com.usernet.product.entity.Comment;
import com.usernet.product.utils.DateUtils;
import com.usernet.product.utils.Page;
import com.usernet.product.utils.PageResult;
import com.usernet.product.utils.ProductConfig;

/**
 * 评论信息
 * 
 * @author wangshuaibo
 * 
 */
public class CommentDao extends BaseDao {

	/**
	 * 查询应用评论条数
	 * 
	 * @param softId
	 *            软件ID
	 * @return 应用评论条数
	 */
	public int getCountBySoftId(int softId) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Comment as comment where comment.status!=? and comment.software.id=?";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, softId);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	/**
	 * 查询应用所有评论
	 * 
	 * @param softId
	 *            软件ID
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码(从1开始)
	 * @return 评论集合
	 */
	@SuppressWarnings("unchecked")
	public List<Comment> getAllBySoftId(int softId, int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select c from Comment as c where c.status !=? and c.software.id=? order by c.time desc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, softId);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Comment> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public PageResult<Comment> getAllByPage(Page page) {
		String hql = "from Comment order by id";
		return super.getAllByPageAndHql(page, hql);
	}

	@SuppressWarnings("unchecked")
	public PageResult<Comment> getAllByParams(Page page, String softName,
			String time) {
		Map<String, Timestamp> map = null;
		String hql = "from Comment where 1=1";
		if (softName != null && !"".equals(softName.trim())) {
			hql += " and (software.name like '%" + softName
					+ "%' or software.pinyin like '%" + softName + "%')";
		}
		if (time != null && !"".equals(time.trim())) {
			hql += " and time>=:beginTime and time<=:endTime";
			Timestamp beginTime = DateUtils.stringToTimestampBegin(time);
			Timestamp endTime = DateUtils.stringToTimestampEnd(time);
			map = new HashMap<String, Timestamp>();
			map.put("beginTime", beginTime);
			map.put("endTime", endTime);
		}
		hql += " order by id";
		return super.getAllByPageAndHql(page, hql, map);
	}
}
